package mest.service.Modeling.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import mest.VO.Modeling.WarehouseSaveFile_manytomany.savefileVO;
import mest.dao.Modeling.IwarehouseFileDao;
import mest.entity.warehouse_file;
import mest.entity.warehousesave_manytomany;
import mest.service.Modeling.IwarehouseFileService;
import mest.service.Modeling.warehousesave_manytomany_recordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-16 1:27
 */
@Service
public class WarehouseFileServiceImpl implements IwarehouseFileService {

    @Resource
    private IwarehouseFileDao iwfd;

    @Resource
    private warehousesave_manytomany_recordService wmr;

    @Override
    public int insertWarehouseFileType(warehouse_file ct, String theirDb) {
        try {
            return iwfd.insertWarehouseFileType(ct, theirDb);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int selectCount(String theirDb) {
        return iwfd.selectCount(theirDb);
    }

    @Override
    public List<warehouse_file> findWarehouseFilePage(int index, int pageSize, String theirDb) {
        return iwfd.findWarehouseFilePage((index - 1) * pageSize, pageSize, theirDb);
    }

    @Override
    public List<savefileVO> findSaveByWFId(int wfId, String commName, int index, int pageSize, String theirDb) {
        return iwfd.findSaveByWFId(wfId, commName, (index - 1) * pageSize, pageSize, theirDb);
    }

    @Override
    public int SaveCount(int wfId, String commName, String theirDb) {
        return iwfd.SaveCount(wfId, commName, theirDb);
    }

    @Override
    public int insertManytomany_WarehouseAndSave(warehousesave_manytomany wsmtm, String theirDb, Integer userId) {
        int i = 0;
        System.out.println("wsmtm:" + wsmtm.toString());
        System.out.println("theirDb:" + theirDb);
        System.out.println("userId:" + userId);
        try {
            //操作入库
            i = iwfd.insertManytomany_WarehouseAndSave(wsmtm, theirDb);
            //入库成功后增加操作记录
            wmr.insertModel(wsmtm, theirDb, userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateManytomany_WarehouseAndSave(savefileVO wsmtm, String theirDb) {
        return iwfd.updateManytomany_WarehouseAndSave(wsmtm, theirDb);
    }

    @Override
    public int delManytomany_WarehouseAndSave(savefileVO wsmtm, String theirDb) {
        return iwfd.delManytomany_WarehouseAndSave(wsmtm, theirDb);
    }

    @Override
    public String examine(Map<String, Object> map) {
        System.out.println("进入查询");
        Map<String, Object> returnmap = new HashMap<String, Object>();
        try {
            Map<String, Object> examine = iwfd.examine(map);
            if (examine == null) {
                returnmap.put("code", 0);
                returnmap.put("count", 0);
                returnmap.put("msg", "可以新增");
            } else {
                returnmap.put("code", 0);
                returnmap.put("count", 1);
                returnmap.put("msg", "已存在");
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            returnmap.put("code", 1);
            returnmap.put("msg", "查询失败");
            returnmap.put("errmsg", e.toString());
        }
        return JSON.toJSONString(returnmap);
    }

    @Override
    public Integer getNumber_WarehouseAndSave(Integer id, String theirDb) {
        return iwfd.getNumber_WarehouseAndSave(id, theirDb);
    }

    @Override
    public Integer updateNumber_WarehouseAndSave(Integer id, Integer number, String theirDb) {
        return iwfd.updateNumber_WarehouseAndSave(id, number, theirDb);
    }

    @Override
    public Integer countNumber_WarehouseAndSave(String status, Integer currentNumber, Integer editNumber) {
        Integer resultNumber = 0;
        if (status.equals("入库")) {
            resultNumber = currentNumber + editNumber;
        } else if (status.equals("出库")) {
            resultNumber = currentNumber - editNumber;
        }
        return resultNumber;
    }
}
